
$(document).ready(init);

var lastEditRowIndex;
var editMode;

function init(){
	initData();
	renderPage();
	bindEvent();
}

function initData(){
	queryMode = _QUERY_MODE_SIMPLE;
}

function bindEvent(){
	$(document).bind(_EVENT_PROMPT_INFO,ComponentUtil.promptMsg);
	$(document).bind(_EVENT_PROMPT_WARNING,ComponentUtil.promptMsg);
	$(document).bind(_EVENT_PROMPT_ERROR,ComponentUtil.promptMsg);
}

function renderPage(){
	
	var addBt = {text:'新增参数',iconCls:'icon-add',handler:addSystemConfig};
	var removeBt = {text:'删除参数',iconCls:'icon-cancel',handler:confirmRemove};
	
	var acceptBt = {text:'保存变更',iconCls:'icon-save',handler:acceptChanges};
	var rejectBt = {text:'取消变更',iconCls:'icon-undo',handler:rejectChanges};
	
	var reloadBt = {text:'刷新',iconCls:'icon-reload',handler:reloadList};
	
	$("#systemConfigTable").datagrid({
		striped: true,
		fitColumns:true,
		fit:true,
		idField:'id',
		frozenColumns:[[
	        {field:'ck',checkbox:true}
	    ]],
	    sortName:'id',
    	sortOrder:'desc',
		url:webappName+'/admin/systemConfigManager/list',
		toolbar:[addBt,'-',removeBt,'-',acceptBt,'-',rejectBt,'-',reloadBt,'-'],
		rowStyler:rowRender,
		onDblClickRow:editSystemConfig,
		onAfterEdit:commitUpdateOrAddSystemConfig,
		loadMsg:'加载数据中,请稍等...'
	});
	
	ComponentUtil.localizePagination($('#systemConfigTable').datagrid('getPager'));
	
}

function rowRender(rowIndex,rowData){
	if (rowData.status == 9){
		return 'background-color:#ffdddd';
	}
}

function statusRender(value,rowData,rowIndex){
	for (var i=0;i<_COMMON_STATUS_COMBO_SELECTOR.length;i++){
		if (value==_COMMON_STATUS_COMBO_SELECTOR[i].value){
			return _COMMON_STATUS_COMBO_SELECTOR[i].text;
		}
	} 
	return '未知('+value+')';
}

function typeRender(value,rowData,rowIndex){
	for (var i=0;i<_SYS_CONFIG_TYPE_SELECTOR.length;i++){
		if (value==_SYS_CONFIG_TYPE_SELECTOR[i].value){
			return _SYS_CONFIG_TYPE_SELECTOR[i].text;
		}
	} 
	return '未知('+value+')';
}


function confirmRemove(){
	var rows = $('#systemConfigTable').datagrid('getSelections');
	if (rows.length==0){
		$(document).trigger(_EVENT_PROMPT_INFO,'请选择需要删除的项目');
		return;
	}
	$.messager.confirm('确认','确认要删除吗?',removeSystemConfig);
}

function removeSystemConfig(isConfirm){
	
	if (!isConfirm) return;
	
	var rows = $('#systemConfigTable').datagrid('getSelections');
	console.debug(rows);
	var sysConf;
	var keyList='';
	for(var i=0;i<rows.length;i++){
		sysConf = rows[i];
		keyList += sysConf.keyName + ',';
	}
	keyList = keyList.substring(0,keyList.length-1);
	
	var requestPara = 'keyList='+keyList;
	console.debug(requestPara);
	$.post(webappName+"/admin/systemConfigManager/remove",requestPara,onRemoveSystemConfigComplete,"json");
}

function onRemoveSystemConfigComplete(data){
	
	$("#systemConfigTable").datagrid('reload');
	
	var result = data.executeResult;
	if (result){
		$(document).trigger(_EVENT_PROMPT_INFO,"操作成功");
	}else{
		$(document).trigger(_EVENT_PROMPT_ERROR,ComponentUtil.getErrorDesc(data.errCode));
	}
	
	$('#systemConfigTable').datagrid('clearSelections');
}

function editSystemConfig(rowIndex){
	
	if (!$('#systemConfigTable').datagrid('validateRow',lastEditRowIndex)){
		return;
	};
	
	
	var opts = $('#systemConfigTable').datagrid('getColumnOption', 'keyName');
	
	opts.editor =null;
	
	if (lastEditRowIndex!=rowIndex){
		$("#systemConfigTable").datagrid('endEdit', lastEditRowIndex);
		$("#systemConfigTable").datagrid('beginEdit', rowIndex);
	}
	lastEditRowIndex = rowIndex;
	
	editMode = _EDIT_MODE_MODIFY;
}

function commitUpdateOrAddSystemConfig(rowIndex, rowData, changes){
	if (editMode==_EDIT_MODE_MODIFY){
		commitSystemConfigUpdate(rowIndex, rowData, changes);
	}else if(editMode==_EDIT_MODE_APPEND){
		commitAddSystemConfig(rowIndex, rowData, changes);
	}
	reloadList();
}

function commitSystemConfigUpdate(rowIndex, rowData, changes){
	
	var changedKeys = [];
	for(var key in changes){
		changedKeys.push(key);
	}
	if(changedKeys.length==0) return;
	
	$.post(webappName+"/admin/systemConfigManager/modify",$.param(rowData),onUpdateSystemConfigComplete,"json");
	$('#systemConfigTable').datagrid('clearSelections');
}

function commitAddSystemConfig(rowIndex, rowData, changes){
	$.post(webappName+"/admin/systemConfigManager/add",$.param(rowData),onAddSystemConfigComplete,"json");
	$('#systemConfigTable').datagrid('clearSelections');
}

function onUpdateSystemConfigComplete(responseVO){
	
	var result = responseVO.executeResult;
	if (result){
		$(document).trigger(_EVENT_PROMPT_INFO,"更新成功");
	}else{
		$(document).trigger(_EVENT_PROMPT_ERROR,ComponentUtil.getErrorDesc(responseVO.errCode));
	}
	
}

function onAddSystemConfigComplete(responseVO){
	var result = responseVO.executeResult;
	if (result){
		$(document).trigger(_EVENT_PROMPT_INFO,"更新成功");
	}else{
		$(document).trigger(_EVENT_PROMPT_ERROR,ComponentUtil.getErrorDesc(responseVO.errCode));
	}
	
}

function addSystemConfig(){
	
	if (!$('#systemConfigTable').datagrid('validateRow',lastEditRowIndex)){
		return;
	};
	
	editMode = _EDIT_MODE_APPEND;
	
	$('#systemConfigTable').datagrid('endEdit', lastEditRowIndex);
	
	$('#systemConfigTable').datagrid('appendRow',{type:'system',status:0});
	lastEditRowIndex = $('#systemConfigTable').datagrid('getRows').length-1;
	
	var opts = $('#systemConfigTable').datagrid('getColumnOption', 'keyName');
	
	opts.editor ={type:'validatebox',options:{required:"true"}};
	
	$('#systemConfigTable').datagrid('beginEdit', lastEditRowIndex);
}

function acceptChanges(){
	$("#systemConfigTable").datagrid('acceptChanges');
	lastEditRowIndex = -1;
}
function rejectChanges(){
	$("#systemConfigTable").datagrid('rejectChanges');
	lastEditRowIndex = -1;
}
function reloadList(){
	$("#systemConfigTable").datagrid('reload');
}
